classdef EYELINK_EXP < handle
    % This class is designe to help the interactions with the eyetracker,
    % in particular by grouping all the methods
    
    properties
        
        % Experiment that called the eyelink
        exp
        % Calibration background
        cb = [0 0 0]
        % Calibration text font
        cf = [255 255 255]
        % Calibration image title
        cit = [255 255 255];
        % Calibration type
        ct = 'HV5'
        % Calibration stimulus center color
        cscc = [ 0 0 0]
        % Calibration stimulus big ball color
        csbbc = [255 255 255]
        
        % TODO Properly
        
        % Power of the infrared illuminator, either 50, 75 or 100
        irp = 75
        % Eye to recorded 0 is left, 1 is right
        er = 1
        % Final
        screen_phys_coords
        screen_pixel_coords
        screen_distance
        % Manual configuration
        mc = 0
    end
    
    
    methods
        
        function self = EYELINK_EXP(exp)
            % Constructor class.
            %
            % EXP   Instance of the class ET_GEN. A pointer to this
            %       instance is stored in the EYELINK_EXP instance.
            
            if ~isa(exp,'ET_GEN')
                error('exp is not an intance of ET_GEN');
            end
            self.exp = exp;
            
        end
        
        function cnfFinal(self)
            
            % EYETRACKER SETUP
            
            Eyelink('Command', ['screen_phys_coords = %.1f, %.1f,' ...
                '%.1f, %.1f'],self.screen_phys_coords);
            % in millimetres
            Eyelink('Command','screen_pixel_coords = %d, %d, %d, %d', ...
                round(self.screen_pixel_coords));
            % screen resolution
            Eyelink('Command', 'screen_distance = %.1f, %.1f',...
                self.screen_distance);
            
        end
        
        function calibrate(self,window)
                        
            % Compulsory
            if nargin < 2
                window = self.exp.scn.p;
            end
                
                     
            el=EyelinkInitDefaults(window);
            
            el.window = window;
            
            el.backgroundcolour         = self.cb;
            el.msgfontcolour            = self.cf;
            el.imgtitlecolour           = self.cit;
            el.targetbeep               = 0;
            el.calibrationtargetcolour  = self.csbbc;
            el.calibrationtargetsize    = 1;
            el.calibrationtargetwidth   = 0.5;
            el.displayCalResults        = 1;
            el.eyeimgsize               = 50;
            EyelinkUpdateDefaults(el);
            
            % Initialization of the connection with the Eyelink Gazetracker.
            % exit program if this fails.
            
            [width, height]=Screen('WindowSize', window);
            
            
            % make sure we're still connected.
            if Eyelink('IsConnected')~=1 
                fprintf('Not connected. exiting');
                cleanup;
                return;
            end
                     
            % set calibration type.
            Eyelink('command', 'calibration_type = HV5');
            % you must send this command with value NO for custom calibration
            % you must also reset it to YES for subsequent experiments
            
            %Eyelink('command', 'generate_default_targets = NO');
            Eyelink('command', 'generate_default_targets = NO');
            
            
            % STEP 5.1 modify calibration and validation target locations
            Eyelink('command','calibration_samples = 6');
            Eyelink('command','calibration_sequence = 0,1,2,3,4,5');
            Eyelink('command','calibration_targets = %d,%d %d,%d %d,%d %d,%d %d,%d',...
                round(width/2),round(height/2),round(width/2),round(height*0.2), ...
                round(width/2),round(height-height*0.2),round(width*0.2),...
                round(height/2),round(width - width*0.2),round(height/2));
            Eyelink('command','validation_samples = 5');
            Eyelink('command','validation_sequence = 0,1,2,3,4,5');
            Eyelink('command','validation_targets = %d,%d %d,%d %d,%d %d,%d %d,%d',...
                round(width/2),round(height/2),round(width/2),round(height*0.2), ...
                round(width/2),round(height-height*0.2),round(width*0.2),...
                round(height/2),round(width - width*0.2),round(height/2));
            
            % allow to use the big button on the eyelink gamepad to accept the
            % calibration/drift correction target
            Eyelink('command', 'button_function 5 "accept_target_fixation"');
            [~, reply]=Eyelink('ReadFromTracker','enable_automatic_calibration');
            
            if reply % reply = 1
                fprintf('Automatic sequencing ON');
            else
                fprintf('Automatic sequencing OFF');
            end
            
            % enter Eyetracker camera setup mode, calibration and validation
            EyelinkDoTrackerSetup(el);
            %Eyelink('command', 'generate_default_targets = YES');
            
        end
    end
    
end